﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;


namespace EpisodeWizard.App.WizardPages
{

	public partial class WizardPagePattern : CoreUI.WizardPage
	{
		public WizardPagePattern()
		{
			InitializeComponent();
			
			CheckNamingPatternIsValid();

			foreach (KeyValuePair<string, Core.TagEngine.TagInfo> Pair in Core.Program.GetInstance().PatternEngine.Tags)
			{
				ListViewItem LVI = new ListViewItem(Pair.Key);
				LVI.SubItems.Add(Pair.Value.Description);
				listTags.Items.Add(LVI);
			}
		}

		/// <summary>
		/// User feedback on whether or not the pattern is valid
		/// </summary>
		private void textPattern_TextChanged(object sender, EventArgs e)
		{
			CheckNamingPatternIsValid();
		}

		/// <summary>
		/// User feedback on whether or not the pattern is valid
		/// </summary>
		private void CheckNamingPatternIsValid()
		{
			bool Valid = Core.Program.GetInstance().PatternEngine.CheckValidPattern(textPattern.Text);
			if (Valid)
			{
				textPattern.BackColor = Color.LightGreen;
			}
			else
			{
				textPattern.BackColor = Color.Red;
			}
		}


		/// <summary>
		/// Computes the new filenames based on the provided pattern
		/// </summary>
		private void WizardPagePattern_LeavePage()
		{
			Core.Program.GetInstance().PatternEngine.ComputeFilenames(textPattern.Text);
			Core.Program.GetInstance().OnFileListValuesChanged();
		}

		/// <summary>
		/// Do not proceed if the pattern is not valid
		/// </summary>
		/// <param name="e"></param>
		private void WizardPagePattern_ValidatePage(ValidatePageEventArgs e)
		{
			bool Valid = Core.Program.GetInstance().PatternEngine.CheckValidPattern(textPattern.Text);
			if (Valid == false)
			{
				e.Cancel = true;
				MessageBox.Show("Please enter a valid pattern to continue", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
			}
		}


	}
}
